package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCommentService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/comment")
public class DtsAdminComment {
    @Autowired
    DtsCommentService commentService;
    @Autowired
    DtsBrandService brandService;

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsComment dtsComment){
        Subject subject = SecurityUtils.getSubject();
        if(!subject.hasRole("超级管理员")){
            return ResponseUtil.fail(-1,"没有权限");
        }
        commentService.delete(dtsComment);
        return ResponseUtil.ok();
    }

    @RequestMapping("/list")
    public Object list(@RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(name = "limit",required = false,defaultValue = "20")Integer limit,
                       String sort,String desc,Integer userId,
                       @RequestParam(name = "valueId",required = false)Integer goodsId){

        Subject subject = SecurityUtils.getSubject();
        DtsAdmin principal = (DtsAdmin) subject.getPrincipal();
        List<Integer> brands=null;
        if(subject.hasRole("品牌制造商")){
             brands = brandService.findByAdminById(principal.getId());
        }
        IPage<DtsComment> iPage=commentService.findPage(page,limit,sort,desc,userId,goodsId,brands);
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }
}
